<?php

if(1){
	header("Content-Type: application/force-download");
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/download");
	header("Content-Transfer-Encoding:utf-8");
	header("Pragma: no-cache");
	header('Content-Type: application/vnd.ms-excel');
	header('Cache-Control: max-age=0');
	header("Content-Disposition:filename=" . "_" . date("Ymd") . time() . ".xls");
}



$titles 	= array("序號", "需求類型", "姓名", "手機", "住家電話", "公司電話", "住家地址", "大樓名稱", "公司地址", "公司名稱", "Email",
					"聯絡人", "聯絡人電話", "物件用途", "建物面積1", "建物面積2", "室內面積1", "室內面積2", "土地面積1", "土地面積2", "車位數", "可接受機械", "重點區域1", "重點區域2", "指定大樓1", "指定大樓2", "路段1", "路段2", "物件案名1", "物件案名2", "捷運線1", "捷運站起點1", "捷運站終點1", "捷運線2", "捷運站起點2", "捷運站終點2", "緯度1", "經度1", "緯度2", "經度2", "公園1", "公園2", "學校1", "學校2", "商圈1", "商圈2", "行政區1", "行政區2", "總預算1", "總預算2", "每坪預算1", "每坪預算2", "投報率1", "投報率2", "承租營業用途", "屋齡", "物件案別", "落地窗座向", "該戶座向", "大樓座向", "指定樓層1", "指定樓層2", "指定樓層", "物件型式", "公共設施", "附屬配備", "管理", "房數1", "房數2", "廳數1", "廳數2", "衛數1", "衛數2", "套房數1", "套房數2", "分區使用", "樓地板高度", "客戶來源", "需求等級", "備注", "幣別", "面積單位", "狀態", "無效說明", "接待人", 
					"指派主管", 
					//"原經紀人", 
					"經紀人", "建檔時間");
//$titles 	= $selsect_fields;
echo '<table border=1 style="font-size:14px;">';
echo '<tr>';
foreach ($titles as $key => $name) {
	echo '<td>';
	echo $name;
	echo '</td>';
}
echo '</tr>';

$_string='';
$i = 1;


foreach ($result as $key => $val) {

	if(1){

		echo '<tr>';
		echo '<td>' . $i . "</td>";
		
		
		$t_str="";
		if($val->type_rent==1){
				$t_str="租";
		}
		if($val->type_sell==1){
				$t_str.="售";
		}
		if($val->type_consignment>0){
				$t_str.="代銷";
		}

		if($t_str)
			echo "<td>".$t_str."</td>";
		else
			echo "<td>-</td>";

		if($val->c_name)
			echo "<td>".$val->c_name."</td>";
		else
			echo "<td>-</td>";

		if($val->c_mobile)
			echo "<td>".$val->c_mobile."</td>";
		//else
		//	echo "<td>-</td>";

		if($val->c_home_phone)
			echo "<td>".$val->c_home_phone."</td>";
		else
			echo "<td>-</td>";

		if($val->c_company_phone)
			echo "<td>".$val->c_company_phone."</td>";
		else 
			echo "<td>-</td>";

		if($val->c_home_address)
			echo '<td>'.$val->c_home_address."</td>";				
		else 
			echo "<td>-</td>";

		if($val->c_home_building_name){
			echo "<td>";
			//echo $val->building_name;

			// foreach ($building_names as  $value ) {
			// 	if($value->building_name_id==$val->building_name )
			// 		echo $value->value;
			// }
			echo $val->c_home_building_name;
				//print_r($building_names);

			echo "</td>";
		}else{
			echo "<td>-</td>";
		}
		if($val->c_company_address)
			echo "<td>".$val->c_company_address."</td>";
		else
			echo "<td>-</td>";
		if($val->c_company_name)
			echo "<td>".$val->c_company_name."</td>";
		else
			echo "<td>-</td>";
		if($val->c_email)
			echo "<td>".$val->c_email."</td>";
		else
			echo "<td>-</td>";




		if($val->c_c_name)
			echo "<td>".$val->c_c_name."</td>";
		else
			echo "<td>-</td>";	
		if($val->c_c_mobile)
			echo "<td>".$val->c_c_mobile."</td>";
		else
			echo "<td>-</td>";

		//if(isset($object_usage[$val->object_usage]))
		//	echo "<td>".$object_usage[$val->object_usage]."</td>";

		if($val->object_usage){
			echo "<td>";
			echo $val->object_usage;
			 
			echo "</td>";
		}else{
			echo "<td>-</td>";
		}
		if($val->building_area1)
			echo "<td>".(int)$val->building_area1."</td>";
		else
			echo "<td>-</td>";

		if($val->building_area2)
			echo "<td>".(int)$val->building_area2."</td>";
		else
			echo "<td>-</td>";
		if($val->indoor_area1)
			echo "<td>".(int)$val->indoor_area1."</td>";
		else
			echo "<td>-</td>";
		if($val->indoor_area2)
			echo "<td>".(int)$val->indoor_area2."</td>";
		else
			echo "<td>-</td>";
		if($val->land_area1)
			echo "<td>".(int)$val->land_area1."</td>";
		else
			echo "<td>-</td>";
		if($val->land_area2)
			echo "<td>".(int)$val->land_area2."</td>";
		else
			echo "<td>-</td>";
		if($val->parking_number)
			echo "<td>".$val->parking_number."</td>";
		else
			echo "<td>-</td>";
		if($val->acceptable_mechanical==1) 
			echo "<td>"."可接受機械"."</td>";
		else 
			echo "<td>-</td>";

		if($val->key_areas1)
			echo "<td>".$val->key_areas1."</td>";
		else
			echo "<td>-</td>";
		if($val->key_areas2)
			echo "<td>".$val->key_areas2."</td>";
		else
			echo "<td>-</td>";
		if($val->specify_building1)
			echo "<td>".$val->specify_building1."</td>";
		else
			echo "<td>-</td>";
		if($val->specify_building2)
			echo "<td>".$val->specify_building2."</td>";
		else
			echo "<td>-</td>";
		if($val->sections1)
			echo '<td>'.$val->sections1,"</td>";		
		else
			echo "<td>-</td>";
		if($val->sections2)
			echo "<td>".$val->sections2."</td>";
		else
			echo "<td>-</td>";
		if($val->object_name1)
			echo '<td>'.$val->object_name1."</td>";				
		else
			echo "<td>-</td>";
		if($val->object_name2)
			echo "<td>".$val->object_name2."</td>";
		else
			echo "<td>-</td>";
		if(isset($mrt_line[$val->mrt_line1]))
			echo "<td>".$mrt_line[$val->mrt_line1]."</td>";
		else
			echo "<td>-</td>";
		if(isset($mrt[$val->mrt_start1]))
			echo "<td>".$mrt[$val->mrt_start1]."</td>";
		else
			echo "<td>-</td>";
		if(isset($mrt[$val->mrt_until1]))
			echo "<td>".$mrt[$val->mrt_until1]."</td>";
		else
			echo "<td>-</td>";
		if(isset($mrt_line[$val->mrt_line2]))
			echo "<td>".$mrt_line[$val->mrt_line2]."</td>";
		else
			echo "<td>-</td>";
		if(isset($mrt[$val->mrt_start2]))
			echo "<td>".$mrt[$val->mrt_start2]."</td>";
		else
			echo "<td>-</td>";
		if(isset($mrt[$val->mrt_until2]))
			echo "<td>".$mrt[$val->mrt_until2]."</td>";
		else
			echo "<td>-</td>";
		if($val->range_east)
			echo "<td>".$val->range_east."</td>";
		else
			echo "<td>-</td>";
		if($val->range_north)
			echo "<td>".$val->range_north."</td>";
		else
			echo "<td>-</td>";
		if($val->range_west)
			echo "<td>".$val->range_west."</td>";
		else
			echo "<td>-</td>";
		if($val->range_south)
			echo "<td>".$val->range_south."</td>";
		else
			echo "<td>-</td>";
		if($val->park1)
			echo "<td>".$val->park1."</td>";
		else
			echo "<td>-</td>";
		if($val->park2)
			echo "<td>".$val->park2."</td>";
		else
			echo "<td>-</td>";
		if($val->school1)
			echo "<td>".$val->school1."</td>";
		else
			echo "<td>-</td>";
		if($val->school2)
			echo "<td>".$val->school2."</td>";
		else
			echo "<td>-</td>";
		if($val->circle1)
			echo "<td>".$val->circle1."</td>";
		else
			echo "<td>-</td>";
		if($val->circle2)
			echo "<td>".$val->circle2."</td>";
		else
			echo "<td>-</td>";
		if($val->administrative_area1){
			echo "<td>".$val->administrative_area1."</td>";
		}else
			echo "<td>-</td>";
		if($val->administrative_area2)
			echo "<td>".$val->administrative_area2."</td>";				
		else
			echo "<td>-</td>";
		if($val->total_budget1)
			echo "<td>".(int)$val->total_budget1."</td>";
		else
			echo "<td>-</td>";
		if($val->total_budget2)
			echo "<td>".(int)$val->total_budget2."</td>";
		else
			echo "<td>-</td>";
		if($val->budget_per_ping1)
			echo "<td>".(int)$val->budget_per_ping1."</td>";
		else
			echo "<td>-</td>";
		if($val->budget_per_ping2)
			echo "<td>".(int)$val->budget_per_ping2."</td>";
		else
			echo "<td>-</td>";
		if($val->reported_rate1)
			echo "<td>".(int)$val->reported_rate1."</td>";
		else
			echo "<td>-</td>";
		if($val->reported_rate2)
			echo "<td>".(int)$val->reported_rate2."</td>";
		else
			echo "<td>-</td>";


		if($val->leasing_business_purposes)
			echo "<td>".$val->leasing_business_purposes."</td>";
		else
			echo "<td>-</td>";



		if($val->year_of_construction)
			echo "<td>".$val->year_of_construction."</td>";
		else
			echo "<td>-</td>";



		
		// $oct="";
		// if(strstr($val->object_case_type,'1')){}
		// 	$oct.="中古屋";
		// }
		// if(strstr($val->object_case_type,'2')){
		// 	if($oct=="")
		// 		$oct.="預售";
		// 	else
		// 		$oct.="/預售";
		// }
		
		// if(strstr($val->object_case_type,'3')){
		// 	if($oct=="")
		// 		$oct.="標售";
		// 	else
		// 		$oct.="/標售";
		// }

		if($val->object_case_type){

			$aasssads=$val->object_case_type;
			echo "<td>";
			$qq=explode(',',$aasssads);

			foreach($qq as $aa =>$aav){

				if($aav==1){
					echo "中古屋/";
				}elseif($aav==2){
					echo "預售/";
				}elseif($aav==3){
					echo "標售/";
				}
			}

			echo "</td>";

		}else{

			echo "<td>-</td>";

		}




		if(  $val->windows ) 
			echo "<td>". $val->windows ."</td>";
		else
			echo "<td>-</td>";
		if( $val->facing_the_household ) 
			echo "<td>". $val->facing_the_household ."</td>";
		else
			echo "<td>-</td>";
		if( $val->building_facing) 
			echo "<td>". $val->building_facing ."</td>";
		else
			echo "<td>-</td>";
		if($val->designated_floors1)
			echo "<td>".$val->designated_floors1."</td>";
		else
			echo "<td>-</td>";
		if($val->designated_floors2)
			echo "<td>".$val->designated_floors2."</td>";
		else
			echo "<td>-</td>";
		if($val->floors)
			echo "<td>".$val->floors."</td>";
		else
			echo "<td>-</td>";
		if( $val->object_styles )
			echo "<td>".$val->object_styles."</td>";
		else
			echo "<td>-</td>";
		if($val->public_facilities)
			echo "<td>". $val->public_facilities."</td>";
		else
			echo "<td>-</td>";
		if($val->accessories_with)
			echo "<td>".$val->accessories_with."</td>";
		else
			echo "<td>-</td>";

		if($val->management)
			echo "<td>".$val->management."</td>";
		else
			echo "<td>-</td>";
		
		if($val->number_of_rooms1)
			echo "<td>".$val->number_of_rooms1."</td>";
		else
			echo "<td>-</td>";
		if($val->number_of_rooms2)
			echo "<td>".$val->number_of_rooms2."</td>";
		else
			echo "<td>-</td>";
		if($val->number_of_hall1)
			echo "<td>".$val->number_of_hall1."</td>";
		else
			echo "<td>-</td>";
		if($val->number_of_hall2)
			echo "<td>".$val->number_of_hall2."</td>";
		else
			echo "<td>-</td>";

		if($val->number_of_toilet1)
			echo "<td>".$val->number_of_toilet1."</td>";
		else
			echo "<td>-</td>";
		if($val->number_of_toilet2)
			echo "<td>".$val->number_of_toilet2."</td>";
		else
			echo "<td>-</td>";

		if($val->number_of_suite1)
			echo "<td>".$val->number_of_suite1."</td>";
		else
			echo "<td>-</td>";



		if($val->number_of_suite2)
			echo "<td>".$val->number_of_suite2."</td>";
		else
			echo "<td>-</td>";

		if( $val->partition_the )
			echo "<td>". $val->partition_the."</td>";
		else
			echo "<td>-</td>";


		if($val->floor_height)
			echo "<td>".(int)$val->floor_height."</td>";
		else
			echo "<td>-</td>";


		if( $val->customer_source)
			echo "<td>". $val->customer_source ."</td>";
		else
			echo "<td>-</td>";


		if(isset($required_level[$val->required_level]))
			echo "<td>".$required_level[$val->required_level]."</td>";
		else
			echo "<td>-</td>";


		if($val->remark)
			echo "<td>".$val->remark."</td>";
		else
			echo "<td>-</td>";


		if(isset($currency[$val->currency]))
			echo "<td>".$currency[$val->currency]."</td>";
		else
			echo "<td>-</td>";



		if(isset($area_unit[$val->area_unit]))
			echo "<td>".$area_unit[$val->area_unit]."</td>";
		else
			echo "<td>-</td>";


		if($val->status==1)
			echo "<td>"."有效"."</td>";
		else
			echo "<td>"."無效"."</td>";


		if($val->invalid_description)
			echo "<td>".$val->invalid_description."</td>";
		else
			echo "<td>-</td>";	

		if($val->front_man){
			echo "<td>";
				foreach ($users as $key => $value) {
					if($val->front_man == $value->users_id){
						echo $value->name;
					}
				}
				//$val->front_man;
			echo "</td>";
		}else{
			echo "<td>-</td>";
		}


		if($val->head){
			echo "<td>";
			foreach ($users as $key => $value) {
					if($val->head == $value->users_id){
						echo $value->name;
					}
				}
			
			echo "</td>";
		}else{
			echo "<td>-</td>";
		}
		
		if($val->original_broker){
			echo "<td>";
				foreach ($users as $key => $value) {
					if($val->original_broker == $value->users_id){
						echo $value->name;
					}
				}
			//echo $val->original_broker;
			echo "</td>";
		}else{
			echo "<td>-</td>";
		}
		
		if($val->broker){

			echo "<td>";
			
				foreach ($users as $key => $value) {
					if($val->broker == $value->users_id){
						echo $value->name;
					}
				}
			
			echo "</td>";

		}else{

			echo "<td>-</td>";

		}
		
		echo "<td>".date("Y/m/d",$val->ctime)."</td>";
		
		
		
		
		echo '</tr>';
	}
	$i++;
}

echo '</table>';
?>